package com.leon.module.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.leon.classify.domain.Area;
import com.leon.classify.domain.FirstClass;
import com.leon.classify.domain.LeftRecommon;
import com.leon.classify.domain.MiddleArea;
import com.leon.classify.domain.RightArea;
import com.leon.classify.domain.SecondClass;
import com.leon.common.dao.CommonDao;

public class ModuleDao extends CommonDao {
	public ModuleDao(){
		
	}
	public void createObject(Object obj){
		this.getHibernateTemplate().saveOrUpdate(obj);
	}
	/**
	 * 得到所有Area
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Area> getAllArea(){
		List <Area> list=new ArrayList<Area>();
		String hql="from Area a order by a.displayOrder";
		list=super.qryObjectByHql(hql, null);
		return list;
	}
	public Area getAreaById(String areaId){
		return (Area) super.getObject(Area.class, areaId);
	}
	
	public LeftRecommon getLeftRecById(String id){
		return (LeftRecommon) super.getObject(LeftRecommon.class, id);
	}
	
	public MiddleArea getMiddleAreaById(String id){
		return (MiddleArea) super.getObject(MiddleArea.class, id);
	}
	
	public RightArea getRightAreaById(String id){
		return (RightArea) super.getObject(RightArea.class, id);
	}
	/**
	 * 清除
	 * @param id
	 */
	public void clearFirstClassForLeft(String id){
		final String hql="update FirstClass fc set fc.leftRecommon.leftRecommonId='' where fc.leftRecommon.leftRecommonId="+id;
		getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				session.createQuery(hql);
				// TODO Auto-generated method stub
				return null;
			}
			
		});
	}
	/**
	 * 删除LeftRecommon
	 * @param id
	 */
	public void delLeftRecById(String id){
		LeftRecommon lr=this.getLeftRecById(id);
		Set<FirstClass>fcs=lr.getFirstClasses();
		if(fcs!=null && fcs.size()>0){//先删除以前的
			Set <FirstClass> olist=new HashSet<FirstClass>();
			for(FirstClass fc:fcs){
				fc.setLeftRecommon(null);
				olist.add(fc);
			}
			lr.setFirstClasses(olist);
			this.createObject(lr);
		}
		super.deleteObject(lr);
	}
	/**
	 * 删除MiddleArea
	 * @param id
	 */
	public void delMiddleAreaById(String id){
		MiddleArea ma=this.getMiddleAreaById(id);
		Set<SecondClass>scs=ma.getSecondClasses();
		if(scs!=null && scs.size()>0){//先删除以前的
			Set <SecondClass> olist=new HashSet<SecondClass>();
			for(SecondClass sc:scs){
				sc.setMiddleArea(null);
				olist.add(sc);
			}
			ma.setSecondClasses(olist);
			this.createObject(ma);
		}
		super.deleteObject(ma);
	}
	/**
	 * 删除RightArea
	 * @param id
	 */
	public void delRightAreaById(String id){
		RightArea ra=this.getRightAreaById(id);
		Set<SecondClass> sas=ra.getSecondClasses();
		if(sas.size()>0 && sas!=null){
			Set <SecondClass> olist=new HashSet<SecondClass>();
			for(SecondClass sc:sas){
				sc.setMiddleArea(null);
				olist.add(sc);
			}
			ra.setSecondClasses(olist);
			this.createObject(ra);
		}
		super.deleteObject(ra);
	}
	public void delAreaById(String id){
		Area area=this.getAreaById(id);
		Set<LeftRecommon>lrs=area.getLeftRecommons();
		Set<MiddleArea>mas=area.getMiddleAreas();
		Set<RightArea>ras=area.getRightAreas();
		if(lrs.size()>0&& lrs!=null){
			for(LeftRecommon lr:lrs){
				this.delLeftRecById(lr.getLeftRecommonId());
			}
		}
		if(mas.size()>0 && mas!=null){
			for(MiddleArea ma:mas){
				this.delMiddleAreaById(ma.getMiddleAreaId());
			}
		}
		if(ras.size()>0 && ras!=null){
			for(RightArea ra:ras){
				this.delRightAreaById(ra.getRightAreaId());
			}
		}
		super.deleteObject(area);
	}
	//返回所有左列推荐
	@SuppressWarnings("unchecked")
	public List<LeftRecommon> getAllLeftRec(){
		List <LeftRecommon> list=new ArrayList<LeftRecommon>();
		String hql="from LeftRecommon ";
		list=super.qryObjectByHql(hql, null);
		return list;
	}
	//返回所有中间模块
	@SuppressWarnings("unchecked")
	public List<MiddleArea> getAllMiddle(){
		List <MiddleArea> list=new ArrayList<MiddleArea>();
		String hql="from MiddleArea ";
		list=super.qryObjectByHql(hql, null);
		return list;
	}
	//返回所有右边模块
	@SuppressWarnings("unchecked")
	public List<RightArea> getAllRight(){
		List <RightArea> list=new ArrayList<RightArea>();
		String hql="from RightArea ";
		list=super.qryObjectByHql(hql, null);
		return list;
	}
}
